package filter.filtertest;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

/**
 * Filterview is a view to test filter - methods.
 * When you click on the screen, a point appears to simulate
 * a new GPS - coordinate.
 * 
 * @author Andreas
 */
public class filterview extends View implements OnTouchListener{
	
	// This is an Array List to save geoCoordinates
    ArrayList<GeoCoordinate> geoCoordinates = new ArrayList<GeoCoordinate>();

    Canvas canvas; 			// Create a Canvas-Object
    Bitmap bitmap; 			// Create a Bitmap-Object
    Paint paint; 			// Create a Paint -Object

    int screenWidth; 		// Saves the screen - width
    int screenHeight; 		// Saves the screen - height

    double lonLeft; 		// Saves the east end
	double lonRight;		// Saves the west end
    double latUp; 			// Saves the north end
    double latDown; 		// Saves the south end

    double aspectRatio; 	// Saves the aspect ratio of the coordinates
    double oldGeoX; 		// Saves the old x-coordinate
    double oldGeoY; 		// Saves the old y-coordinate

    boolean init = false;	// Program started?
    
    double centerX = 0;		// Center all point on the x - axis
    double centerY = 0;		// Center all point on the y - axis
    
    double onTouchPointX=0;	// Saves the last click - position
    double onTouchPointY=0;	// Saves the last click - position
    double onTouchLon;		// Saves the click - coordinate
    double onTouchLat;		// Saves the click - coordinate

    public filterview(Context context) {
        super(context);
        initMap();
    }

    public filterview(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttr(attrs);
        initMap();
    }

    public filterview(Context context, AttributeSet attrs, int defStyle) {
        super(context);
        initAttr(attrs);
        initMap();
    }

    private void initAttr(AttributeSet attrs) {
        // example: TypedArray a = getContext().obtainStyledAttributes(attrs,
        // R.styleable.GameBoard);
        // example: this.cols = a.getInteger( R.styleable.GameBoard_cols,12);
        // example: this.rows = a.getInteger( R.styleable.GameBoard_rows,20);
        // example: a.recycle();
    }

    /**#####################################################
     * onDraw draw all track - geoCoordinates to the screen.
     #######################################################*/
    protected void onDraw(Canvas canvas) {

        if (!init){initMap();}

        if(init){
        	
        	float xPoint;
        	float yPoint;
        	
        	// Draw the object.
        	canvas.drawBitmap(bitmap, 0, 0, paint);

        	// Fill the whole background with a specified color.
        	paint.setColor(Color.rgb(200, 255, 200));
        	canvas.drawRect(0, 0, getWidth(), getHeight(), paint);

        	// ###########################################################
        	// Now place the coordinates to the screen. There are x steps.
        	// Step 1: Define the color:
        	paint.setColor(Color.BLUE);
        	
        	// Step 2: We start to draw all coordinates to the screen.
        	for (GeoCoordinate p : geoCoordinates) {
        		
        		// Step 3: Calculate where the point will drawn depend on display-size and area.
        		xPoint = (float)(((p.longitude + lonLeft * (-1))) * (screenWidth /(lonRight - lonLeft)));
        		yPoint = (float)((p.latitude * (-1) + latUp) * (screenHeight / (latUp - latDown)));
        		
        		// Step 4: Maybe we scrolled the map or we just want to center it. So add the value.
        		xPoint += centerX;
        		yPoint += centerY;
        		
        		// Step 5: Finally place the point to the screen.
        		canvas.drawCircle(xPoint, yPoint, 3, paint);
        		
        		// <Did we want to draw lines between the points?>
        		if(true){
        			// Step 6: Now draw a line between the new point and the last point.
        			if(p != geoCoordinates.get(0)){
        				canvas.drawLine(
        						xPoint, 
        						yPoint,
        						(float) ((((oldGeoX + lonLeft * (-1))) * (screenWidth / (lonRight - lonLeft)))+centerX),
        						(float) (((oldGeoY * (-1) + latUp) * (screenHeight / (latUp - latDown)))+centerY),
        						paint);
        			}
        		
        			// Step 7: Remember the new point coordinates. We need them for the next line.
        			oldGeoX = p.longitude;
        			oldGeoY = p.latitude;
        		}
        	}
        }
        
        // ###########################################################

    }

	/**##################################
     * initMap() initializes the MapView
     ####################################*/
    private void initMap() {

        // Setup for OnTouch - Events
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setOnTouchListener(this);
        
        screenWidth	 = getWidth();
        screenHeight = getHeight();
        
        if(screenWidth > 0 && screenHeight > 0){
        	
       		while(geoCoordinates.size() == 0){
       			
       			// This is a first test point. Later we have to ask for 
       			// a first real GPS - coordinate so we can initialize the view.
               	geoCoordinates.add(new GeoCoordinate(44,45));
           	
       		}
           	
           	lonLeft  = geoCoordinates.get(0).longitude - 0.0001;
           	lonRight = geoCoordinates.get(0).longitude + 0.0001;
           	latUp  	 = geoCoordinates.get(0).latitude + 0.0001;
           	latDown  = geoCoordinates.get(0).latitude - 0.0001;
           	
           	// Define the first point
           	oldGeoX = geoCoordinates.get(0).longitude;
           	oldGeoY = geoCoordinates.get(0).latitude;
        
        	// Define local variables
        	paint = new Paint();
        
        	// The bitmap object is created with the screen information.
        	bitmap = Bitmap.createBitmap(screenWidth, screenHeight, Config.RGB_565);

        	// Define the canvas - object.
        	canvas = new Canvas();
        	canvas.setBitmap(bitmap);

        	init = true;
        	
        }
        
    }
    
    @Override
	public boolean onTouch(View view, MotionEvent motionEvent) {
		
		switch (motionEvent.getAction()) {

			case MotionEvent.ACTION_DOWN:{
				
				// Get/Set click - coordinates
				// ###################################################################################
				// Get click longitude
				onTouchLon = motionEvent.getX() - centerX;
				onTouchLon = (onTouchLon / (screenWidth / (lonRight - lonLeft))) - lonLeft * (-1);
				
				// Get click latitude
				onTouchLat = motionEvent.getY() - centerY;
				onTouchLat = ((onTouchLat / (screenHeight / (latUp - latDown))) - latUp) * (-1);
				
				// <This is a test for the on-the-fly-mode>
				geoCoordinates.add(new GeoCoordinate(onTouchLon, onTouchLat));
				lonLeft  = geoCoordinates.get(geoCoordinates.size()-1).longitude - 0.0001;
            	lonRight = geoCoordinates.get(geoCoordinates.size()-1).longitude + 0.0001;
            	latUp  	 = geoCoordinates.get(geoCoordinates.size()-1).latitude + 0.0001;
            	latDown  = geoCoordinates.get(geoCoordinates.size()-1).latitude - 0.0001;
				// ###################################################################################
					
			}
			
			invalidate();  
			
		}
		
		return true;
		
	}

}
